# 爬取车评网站某汽车的评论数据；
# 车评网站任选其一：有驾、懂车帝、易车网、汽车之家、瓜子二手车；
# 分析页面，结合正则化，爬取评论数据，保存为csv文件，字段不限，数据量不少于1w条

# 步骤如下:
# 目标爬取懂车帝的评论数据
# 1.加载api文件和Cookie文件
# 2.查看api地址的调用规律
# 3.写好爬取代码
# 4.通过api调用获取数据
# 5.导入D.csv文件

import time
import csv
import requests

# 读取url
u = open("api.txt", mode="r")
url = u.readline()
u.close()

# 读取Cookie
cokf = open("Cookie.txt", mode="r")
Cookies = cokf.read()
cokf.close()


def get_comments():
    header = {
        "Cookie": Cookies,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                      "AppleWebKit/537.36 (KHTML, like Gecko) "
                      "Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
    }
    for i in range(0, 12000, 30):
        resp = requests.get(url.format(i), headers=header)
        data = resp.json()
        for one in data['data']['cheyou_list']:
            gid = one['gid']
            content = one['content']
            csvwriter.writerow([gid, content])
        time.sleep(1)


if __name__ == '__main__':
    # 计时
    t1 = time.time()
    # 初始化帖子数据文件
    f = open("bak/I.csv", mode="w", encoding="utf-8")
    csvwriter = csv.writer(f)
    csvwriter.writerow(["ID", "用户帖子"])
    # 执行爬取
    get_comments()

    t2 = time.time()
    f.close()
    print("over!")
    print(f"花费时间为:{t2 - t1:.2f}秒")
